﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WinFormClient.ServiceReference1;
using System.Runtime.InteropServices;
namespace WinFormClient
{
    public partial class ChatForm : Form
    {
        public Client client;
        public Topic topic;
        public ChatForm(Topic t,Client c)
        {
            InitializeComponent();
            this.client = c;
            this.topic = t;
            this.Text = "聊天 - " + t.title;
            client.FinishedLoadReplies += new EventHandler(client_FinishedLoadReplies);
            client.FetchReplies(t.id, 30);
        }


        #region (窗口透明代码)
        [StructLayout(LayoutKind.Sequential)]
        public struct MARGINS
        {
            public int Left;
            public int Right;
            public int Top;
            public int Bottom;
        }

        [DllImport("dwmapi.dll", PreserveSig = false)]
        static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);

        [DllImport("dwmapi.dll", PreserveSig = false)]
        static extern bool DwmIsCompositionEnabled();

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (DwmIsCompositionEnabled())
            {
                MARGINS margins = new MARGINS();
                margins.Right = margins.Left = margins.Top = margins.Bottom = 10;//this.Height + this.Width;
                DwmExtendFrameIntoClientArea(this.Handle, ref margins);
            }

        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
            if (DwmIsCompositionEnabled())
            {
                e.Graphics.Clear(SystemColors.Control);
            }
        }
        #endregion
        

        void client_FinishedLoadReplies(object sender, EventArgs e)
        {
            MessageSet msgs = sender as MessageSet;
            if (msgs.topic.id == topic.id)
            {
                foreach (ServiceReference1.Message msg in msgs.Messages)
                {
                    MsgWrite(msg);
                }
                TxtMessage.Focus();
            }
        }
        
//写消息
        private void MsgWrite(ServiceReference1.Message m)
        {
            strwrite("  " + m.userName + "  <" + m.time.ToString() + ">" + "\n", Color.Blue);
            strwrite(m.content + "\n", Color.Black, 13);               
        }

        private void strwrite(string s,Color c,float fontsize = 10)
        {
            int beg = RtxRcvedMsg.TextLength;
            RtxRcvedMsg.AppendText(s);
            int end = RtxRcvedMsg.TextLength;
            RtxRcvedMsg.Select(beg, end - beg);
            RtxRcvedMsg.SelectionColor = c;
            RtxRcvedMsg.SelectionFont = new System.Drawing.Font("宋体", fontsize);
            RtxRcvedMsg.Select(RtxRcvedMsg.Text.Length,0);
        }
        private void ChatForm_Load(object sender, EventArgs e)
        {

        }
        //发送系列
        private void BtnSend_Click(object sender, EventArgs e)
        {
            client.AddReply(topic.id, TxtMessage.Text);
            TxtMessage.Clear();
        }

        private void ChatForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape) this.Hide();
            if (e.KeyCode == Keys.Enter&& e.Control)
            {
                BtnSend_Click(null, null); //执行单击dakai按钮的单击事件
            }
        }

        private void BtnClear_Click(object sender, EventArgs e)
        {
            TxtMessage.Clear();
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
        //发送系列结束

        protected override void OnClosing(CancelEventArgs e)
        {
            //base.OnClosing(e);
            this.Hide();
            e.Cancel = true;            
        }
    }
}
